using System;
using eShop.Business;
using System.Collections.Generic;
using Ext.Net;
using eShop.Model;


public partial class admin_customer : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindCustomers(String.IsNullOrEmpty(e.Sort) ? "Id" : e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }


    private void BindCustomers(string sort, int start, int limit)
    {
        List<User> pagedCustomer = null;
        int totalCount = 0;
        start = start + 1;
        string _searchCustomerName = txtToolbarSearch.Text;
        bool isAsc = sort.Contains("asc") ? true : false;
        pagedCustomer=UserManager.GetUsersPaged(_searchCustomerName, sort, isAsc, start, limit);
        totalCount = UserManager.GetUsersPagedCount(_searchCustomerName);
        Store1.DataSource = pagedCustomer;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void DeleteCustomers(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        //UserManager..DeleteCustomerUser(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void SearchCustomer()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
